package com.example.studyproject.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @program: StudyProject
 * @description: 全局异常处理：当存在多个异常时，匹配，匹配顺序：当前类及其子类向上依次匹配
 * @author: kangwei
 * @create: 2023-03-13 16:54
 **/

@RestControllerAdvice
public class GlobalExceptionAdvice {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionAdvice.class);


    /**
     * exception异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Response exception(Exception e) {
        log.error("exception error", e);
        return Response.error();
    }

    /**
     * 数组异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public Response arithmeticException(ArithmeticException e) {
        log.error("arithmetic exception error", e);
        return Response.error();
    }

    /**
     * 空指针异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    public Object nullException(NullPointerException e) {
        log.error("arithmetic exception error", e);
        return Response.error();
    }
}
